﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;
using Standards.Model.Models;

namespace Standards.Data.EntityFramework.Configurations
{
    public class RolesConfiguration : EntityTypeConfiguration<RoleDto>
    {
        public RolesConfiguration()
        {
            this.ToTable("Roles");

            this.Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.Property(r => r.Name).HasMaxLength(200).IsRequired();   // TODO: check microsoft schema

            this.HasMany(r => r.Users).WithMany(u => u.Roles).Map(cfg =>
                {
                    cfg.ToTable("UsersInRoles");
                    cfg.MapLeftKey("UserId");
                    cfg.MapRightKey("RoleId");
                });

            this.HasMany(r => r.Permissions).WithMany(p => p.Roles).Map(cfg =>
                { 
                    cfg.ToTable("RolePermissions");
                    cfg.MapLeftKey("PermissionId");
                    cfg.MapRightKey("RoleId");
                });
        }
    }
}